<template>
	<view class="page_wrapper">
		<view class="body_block">
			<block v-if="data.originImgUrl">
				<view class="images_block">
					<image :src="data.originImgUrl" mode="aspectFill" class="user_exam_img" @tap="previewImage(data.originImgUrl)" />
					<image :src="data.newImgUrl" mode="aspectFill" class="user_exam_img" @tap="previewImage(data.newImgUrl)" />
				</view>
				<view class="data_stat_block">
					<view class="count_block total_count">总题数: <view class="count">{{data.rightCount + data.errorCount}}</view></view>
					<view class="count_block right_count">答对数: <view class="count">{{data.rightCount}}</view></view>
					<view class="count_block error_count">答错数: <view class="count">{{data.errorCount}}</view></view>
				</view>
			</block>
			<block v-else>
				<view class="prompt_title">请拍照上传口算题照片</view>
			</block>
		</view>
		<view class="btns_block">
			<view class="upload_btn" @click="uploadImg()">拍照上传口算题</view>
		</view>
	</view>
</template>

<script>
	import {request} from '@/utils/request.js'
	import {storageData} from '@/utils/storageData.js'
	export default {
		data() {
			return {
				data: {
					originImgUrl: '',
					newImgUrl: '',
					rightCount: 0,
					errorCount: 0,
					totalCount: 0
				}
			}
		},
		methods: {
			uploadImg() {
				const url = '/aiScans'
				request.uploadImg(url).then(res => {
					const data = JSON.parse(res)
					this.data = data
					this.data.rightCount = parseInt(data.rightCount + '')
					this.data.errorCount = parseInt(data.errorCount + '')
				})
			},
			previewImage(url) {
				uni.previewImage({
					urls: [url],
					longPressActions :{
						itemList:['保存图片'],
						success(data) {
							let imgurl = url
							that.saveImage(url)
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				})
			},
			saveImage(imgurl){
				// console.log(imgurl)
				uni.downloadFile({
					url:imgurl,
					success(res){
						// console.log(res)
						let url = res.tempFilePath
						uni.saveImageToPhotosAlbum({
							filePath:url,
							success() {
								uni.showToast({
									title:'保存成功',
									icon:"none"
								})
							},
							fail(err) {
								console.log('保存失败',err)
								uni.showToast({
									title:'保存失败',
									icon:"none"
								})
							}
						})
					}
				})
			}
		},
		onShareAppMessage(res) {
			const userInfo = storageData.getLoginUser()
			console.log(userInfo)
			if (res.from === 'button') {
					 // 来自页面内分享按钮
					 console.log(res.target);
			}
			return {
				title: '萌芽学口算：AI口算批改',
				path: '/pages/aiScan/aiScan?shareUc=' + userInfo.userCode,
				mpId: 'wxe9f0922a16331d4b', //此处配置微信小程序的AppId
			}
		},
		onShareTimeline(res) {
			const userInfo = storageData.getLoginUser()
			return {
				title: '萌芽学口算：AI口算批改',
				path: '/pages/aiScan/aiScan?shareUc=' + userInfo.userCode,
				mpId: 'wxe9f0922a16331d4b', //此处配置微信小程序的AppId
			}
		},
	}
</script>

<style lang="scss" scoped>
	.page_wrapper{
		.body_block{
			flex: 1;
			overflow-y: scroll;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			.images_block{
				width: 100%;
				display: flex;
				overflow: hidden;
				.user_exam_img{
					flex: 1;
				}
			}
			.data_stat_block{
				display: flex;
				flex-direction: column;
				font-size: 17px;
				margin: 10px 50px;
				.count_block{
					letter-spacing: 5px;
					display: flex;
					align-items: center;
					.count{
						font-size: 20px;
					}
				}
			}
			.prompt_title{
				font-size: 24px;
				font-weight: 500;
				color: #666666;
			}
		}
		.results_block{
			display: flex;
			font-size: 17px;
			margin-bottom: 10px;
		}
		.btns_block{
			height: 60px;
			padding-left: 20px;
			padding-right: 20px;
			display: flex;
			justify-content: center;
			align-items: center;
			.upload_btn{
				flex: 1;
				height: 40px;
				color: white;
				background-color: #67C23A;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 5px;
			}
		}
	}

</style>
